﻿using FBroSharp.Lib;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wss测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //创建浏览器
            BrowserDis.CreatBrowser("", "http://coolaf.com/tool/chattest", this.Handle, 0, this.menuStrip1.Height + 64, this.ClientSize.Width, this.ClientSize.Height - 64);

            
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < BrowserList.data.Count; i++)
            {
                BrowserList.data[i].MoveWindow(0, this.menuStrip1.Height + 64, this.ClientSize.Width, this.ClientSize.Height - 64, true);

            }
        }
    
        private void 载入地址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string url = Interaction.InputBox("请输入地址", "输入框", "http://coolaf.com/tool/chattest", -1, -1);
            BrowserList.data[0].GetMainFrame().LoadURL(url);
        }

        private void 创建服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FBroSharpServerHandle callback = new FBroSharpServerHandle();
            FBroSharpServer.CreateServer("127.0.0.1", 8888, 100, callback);
        }

        private void enableWSSbutton_Click(object sender, EventArgs e)
        {
            var vipControl = BrowserList.data[0].GetVIPControl();
            if (vipControl == null || !vipControl.IsValid)
                return;
            //启用wss功能，这个也可以用在事件OnAfterCreated中，浏览器创建起就生效wss
            vipControl.EnableWebsocketFiter();
            //刷新浏览器，使wss生效
            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void changeWssUrlbutton_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketchangeurl", Encoding.Default.GetBytes(changeWssUrltextBox.Text));
        }

        private void clearAllChangebutton3_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketclear", Encoding.Default.GetBytes("websocketclear"));
        }

        private void changeWssTextbutton_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketchangetext", Encoding.Default.GetBytes(changeWsstextBox.Text));
        }

        private void sendWssTextbutton_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketsendtext", Encoding.Default.GetBytes(sendWsstextBox.Text));
        }

        private void changeWssBitDatabutton_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketchangedata", Encoding.Default.GetBytes(changeWsstextBox.Text));
        }

        private void sendWssBitDatabutton_Click(object sender, EventArgs e)
        {
            var processMessageCon = BrowserList.data[0].GetOwnProcessMessageControl();
            if (processMessageCon == null || !processMessageCon.IsValid)
                return;
            processMessageCon.ServerSendByBrowser("websocketsenddata", Encoding.Default.GetBytes(sendWsstextBox.Text));
        }

        public static MessageForm messageForm = null;
        private void button8_Click(object sender, EventArgs e)
        {
            if (messageForm == null)
                messageForm = new MessageForm();

            messageForm.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            (new Form2()).Show();
        }
    }
}
